var iconUrl=require('../../config.js').iconUrl;
var validata = require('../../utils/validata.js');
Page({
  register: function () {
    var mobile = this.data.mobile;
    var code = this.data.code;
    var psw = this.data.psw;
    var psw1 = this.data.psw1;
    var nick_name = this.data.nick_name;
    var sex = this.data.sex;
    var icon = this.data.icon;
    validata.register(mobile, code, psw, psw1,nick_name,icon,sex);
  }, 
  input: function (e) {
    var key = e.currentTarget.id;
    var value = e.detail.value;
    this.setData({
      [key]: value
    });
  },

  get_code: function (e) {
    var mobile = this.data.mobile;
    var that = this;
    if (this.data.mobile.length !== 11) {
      validata.error_msg("电话号码格式错误");
      return;
    }
    if (this.data.tapable) {
      validata.get_code(mobile,1, function () {
        that._get_code(that);
      });
    }

  },

  _get_code: function (that) {
    var second = that.data.second;
    if (second == 0) {
      that.setData({ second: 60, textlable: "获取验证码", tapable: true });
      clearTimeout();
    } else {
      setTimeout(function () {
        second = second - 1;
        that.setData({ second: second, textlable: second + "s", tapable: false });
        that._get_code(that);
      }, 1000);
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    iconUrl:iconUrl,
    mobile: '',
    code: '',
    real_code: 'xxxx',
    psw: '',
    psw1: '',
    tapable: true,
    second: 60,
    textlable: "获取验证码",
    icon:'',
    nick_name:'',
    sex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this;
     wx.getUserInfo({
        success:function(res){
          var userInfo = res.userInfo;
          that.setData({
            icon: userInfo.avatarUrl,
            nick_name:userInfo.nickName,
            sex:userInfo.gender
          });         
        }
     })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})